package com.bw.server.device




import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.path.RouterPath
import com.bw.common.event.CustomEvent
import com.bw.common.view.BaseMvvmActivity
import com.bw.server.R
import com.bw.server.adapter.DevicesImgAdapter
import com.bw.server.databinding.ActivityAddDeviceBinding
import com.bw.server.entity.AddDeviceEntity
import com.bw.server.entity.AddDeviceEntityNull
import com.bw.server.vm.ServerViewModel
import com.github.gzuliyujiang.wheelpicker.DatePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.entity.DateEntity
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus

/**
 * 新增增设备工单
 */
@AndroidEntryPoint
@Route(path = "/server/AddDeviceActivity")
class AddDeviceActivity : BaseMvvmActivity<ActivityAddDeviceBinding>() {

    private val mViewModel: ServerViewModel by viewModels()
    override fun getLayoutId(): Int = R.layout.activity_add_device
    //图片适配器
    val imgAdapter = DevicesImgAdapter()

    override fun initViews() {
        super.initViews()
        mDataBinding.back.setOnClickListener {
            finish()
        }
        //时间选择器
        mDataBinding.devicesTimeSelect.setOnClickListener {
            val dataPicker = DatePicker(this)
            dataPicker.setTitle("请选择时间")
            dataPicker.wheelLayout.setRange(DateEntity.target(1900,1,1),DateEntity.today())
            dataPicker.setOnDatePickedListener { year, month, day ->
                val date = "${year}年 ${month}月 ${day}日"
                mDataBinding.devicesTime.setText(date)
            }
            dataPicker.show()
        }
        //类型选择器
        mDataBinding.devicesTypeSelect.setOnClickListener {
            val list = mutableListOf("门禁设备","监控设备","其他")
            val optionPicker =OptionPicker(this)
            optionPicker.setData(list)
            optionPicker.setTitle("选择工单类型")
            optionPicker.setOnOptionPickedListener { position, item ->
                mDataBinding.devicesType.setText(list[ position])
            }
            optionPicker.show()
        }

        //地址选择器
        mDataBinding.devicesAddressSelect.setOnClickListener {
            val list = mutableListOf("北门","东门","西门","南门","其他")
            val optionPicker =OptionPicker(this)
            optionPicker.setData(list)
            optionPicker.setTitle("选择具体位置")
            optionPicker.setOnOptionPickedListener { position, item ->
                mDataBinding.devicesAddress.setText(list[ position])
            }
            optionPicker.show()
        }

        //点击提交
        mDataBinding.submitDevice.setOnClickListener {
            mViewModel.addDevice(
                AddDeviceEntityNull(
                    address = mDataBinding.devicesAddress.text.toString(),
                    appointmentTime = mDataBinding.devicesTime.text.toString(),
                    content = mDataBinding.devicesContent.text.toString(),
                    inspector = "张三",
                    orderType = mDataBinding.devicesType.text.toString(),
                    status = "新建")
            )


            mViewModel.mAddSuccess.observe(this){
                ToastUtils.showLong("提交成功")
                finish()
            }

        }

        //点击添加图片
        mDataBinding.addImg.setOnClickListener {
            if (imgAdapter.data.size<4) {
                PictureSelector.create( this,200).selectPicture()
            }else{
                ToastUtils.showLong("最多添加4张图片")
            }
        }
        mDataBinding.rvImg.layoutManager= GridLayoutManager(this,3)
        mDataBinding.rvImg.adapter=imgAdapter
        mDataBinding.rvImg.visibility= View.VISIBLE

    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 200 && resultCode == RESULT_OK) {
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            imgAdapter.addData(bean?.path!!)
            imgAdapter.notifyDataSetChanged()
        }
    }


    //观察数据变化
    override fun observeDataChanged() {
        super.observeDataChanged()
        mViewModel.mAddSuccess.observe(this){
            EventBus.getDefault().post(
                CustomEvent(RouterPath.PATH_DEVICE_PATH,1,1)
            )
            ToastUtils.showLong("报修提交成功")
            finish()
        }
    }


}

















